package com.freight.tws.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.tws.api.domain.PtQyApp;
import com.freight.tws.service.IPtQyAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.freight.common.core.web.controller.BaseServiceImpl;
import com.freight.tws.mapper.PtQyMapper;
import com.freight.tws.api.domain.PtQy;
import com.freight.tws.service.IPtQyService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运营方信息(平台运营方)Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-18
 */
@Service
public class PtQyServiceImpl extends BaseServiceImpl<PtQyMapper, PtQy> implements IPtQyService {

    @Autowired
    private PtQyMapper ptQyMapper;

    @Autowired
    private IPtQyAppService ptQyAppService;

    /**
     * 查询企业信息
     * @return
     */
    @Override
    public PtQy lists() {
        PtQy ptQy = ptQyMapper.selectOne(new LambdaQueryWrapper<PtQy>());
        List<PtQyApp> apps = ptQyAppService.list();
        ptQy.setApps(apps);
        return ptQy;
    }

    /**
     * 获取运营方信息(平台运营方)详细信息
     */
    @Override
    public PtQy getInfo(Long id) {
        PtQy ptQy = ptQyMapper.selectById(id);
        List<PtQyApp> apps = ptQyAppService.list();
        ptQy.setApps(apps);
        return ptQy;
    }

    /**
     * 修改运营方信息(平台运营方)
     */
    @Override
    @Transactional
    public boolean edit(PtQy ptQy) {
        int i = ptQyMapper.updateById(ptQy);
        if(i == 0) {
            return false;
        }
        ptQyAppService.deleteAll();
        for (PtQyApp app : ptQy.getApps()) {
            ptQyAppService.save(app);
        }
        return true;
    }

}
